In Dart, nullability is a part of the type system. By default, any given type T
is non-nullable. If you append a "?" to the type, it
becomes nullable: T?
.
When accessing properties or functions of a nullable type, you need to handle the case when the target is null
. However, while
accessing a non-nullable type, it is redundant to test for null
, as the compiler statically ensures that the value can never be
null
. So all the nullability checks on the non-nullable types are considered code smells.
When overriding an equality operator it doesn’t make sense to check for null, since you can directly check that the object is of a required type.
In this example we can see that other is A
will be true only if other
is not null
.
class A {
final String? value;
@override
operator ==(Object? other) =>
other is A && value == other.value;
}